home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WW3DKit / gnoise.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-22  |  2.0 KB  |  89 lines

  1. #include "proctext.h"
  2. #include "noise.h"
  3.  
  4. #define SMOOTHSTEP(x)  ((x)*(x)*(3 - 2*(x)))
  5.  
  6. static float gradientTab[TABSIZE*3];
  7.  
  8. static void gradientTabInit(int seed);
  9. static float glattice(int ix, int iy, int iz, float fx, float fy, float fz);
  10.  
  11. float
  12. gnoise(float x, float y, float z)
  13. {
  14.     int ix, iy, iz;
  15.     float fx0, fx1, fy0, fy1, fz0, fz1;
  16.     float wx, wy, wz;
  17.     float vx0, vx1, vy0, vy1, vz0, vz1;
  18.     static int initialized = 0;
  19.  
  20.     if (!initialized) {
  21.         gradientTabInit(665);
  22.         initialized = 1;
  23.     }
  24.  
  25.     ix = FLOOR(x);
  26.     fx0 = x - ix;
  27.     fx1 = fx0 - 1;
  28.     wx = SMOOTHSTEP(fx0);
  29.  
  30.     iy = FLOOR(y);
  31.     fy0 = y - iy;
  32.     fy1 = fy0 - 1;
  33.     wy = SMOOTHSTEP(fy0);
  34.  
  35.     iz = FLOOR(z);
  36.     fz0 = z - iz;
  37.     fz1 = fz0 - 1;
  38.     wz = SMOOTHSTEP(fz0);
  39.  
  40.     vx0 = glattice(ix,iy,iz,fx0,fy0,fz0);
  41.     vx1 = glattice(ix+1,iy,iz,fx1,fy0,fz0);
  42.     vy0 = LERP(wx, vx0, vx1);
  43.     vx0 = glattice(ix,iy+1,iz,fx0,fy1,fz0);
  44.     vx1 = glattice(ix+1,iy+1,iz,fx1,fy1,fz0);
  45.     vy1 = LERP(wx, vx0, vx1);
  46.     vz0 = LERP(wy, vy0, vy1);
  47.  
  48.     vx0 = glattice(ix,iy,iz+1,fx0,fy0,fz1);
  49.     vx1 = glattice(ix+1,iy,iz+1,fx1,fy0,fz1);
  50.     vy0 = LERP(wx, vx0, vx1);
  51.     vx0 = glattice(ix,iy+1,iz+1,fx0,fy1,fz1);
  52.     vx1 = glattice(ix+1,iy+1,iz+1,fx1,fy1,fz1);
  53.     vy1 = LERP(wx, vx0, vx1);
  54.     vz1 = LERP(wy, vy0, vy1);
  55.  
  56.     return LERP(wz, vz0, vz1);
  57. }
  58.  
  59. static void
  60. gradientTabInit(int seed)
  61. {
  62.     float *table = gradientTab;
  63.     float z, r, theta;
  64.     int i;
  65.     extern long random();
  66.     extern long srandom();
  67.     
  68.  
  69.     srandom(seed);
  70.     for(i = 0; i < TABSIZE; i++) {
  71.         z = 1. - 2.*RANDNBR;
  72.         /* r is radius of x,y circle */
  73.         r = (float)sqrt((double)(1 - z*z));
  74.         /* theta is angle in (x,y) */
  75.         theta = 2 * M_PI * RANDNBR;
  76.         *table++ = r * (float)cos((double)theta);
  77.         *table++ = r * (float)sin((double)theta);
  78.         *table++ = z;
  79.     }
  80. }
  81.  
  82. static float
  83. glattice(int ix, int iy, int iz,
  84.     float fx, float fy, float fz)
  85. {
  86.     float *g = &gradientTab[INDEX(ix,iy,iz)*3];
  87.     return g[0]*fx + g[1]*fy + g[2]*fz;
  88. }
  89.